(function ($) {
    $.extend({
        comment: {
            init: function (options) {
                $.ajax({
                    url: ctx + "system/comment/load",
                    data: {
                        dataType: options.dataType,
                        dataId: options.dataId,
                        tenantId: options.tenantId,
                        pageNum: options.pageNum,
                        pageSize: options.pageSize
                    },
                    ansyc: false,
                    type: "get",
                    success: function (res) {
                        //还原输入框高度
                        $('#commentTextarea').attr('style', 'overflow-y:hidden;');
                        $("#commentList").empty();
                        if (res.count > 0) {
                            $("#commentList").show();
                            $('#pc-page').show();
                            $('#noComment').hide();
                            var tempalte = $("#commentTemplate").html();
                            $.each(res.fileList, function (index, element) {
                                var removeBtn = '';
                                if(res.currentUserId != null && element.authorIdStr != null && res.currentUserId == element.authorIdStr){
                                    removeBtn = '<span class="common-comment-item-remove" onclick="removeComment(\''+element.idStr+'\')">删除</span>'
                                }
                                var usrImg = (element.userImg == null) || (element.userImg == "") ? ctx + 'img/profile.jpg' : pathToUrl(element.userImg);
                                var one = tempalte.replace("$userName$", element.userName)
                                    .replace("$userImg$", usrImg)
                                    .replace("$commentFloor$", element.commentFloor)
                                    .replace("$content$", element.content)
                                    .replace("$publishDate$", element.publishDate)
                                    .replace("$removeComment$", removeBtn);
                                $("#commentList").append(one);
                            });

                            var totalPages = parseInt((res.count + options.pageSize - 1) / options.pageSize);
                            /* 分页 */
                            $('#pc-page').paging({
                                initPageNo: options.pageNum, // 初始页码，默认为1
                                totalPages: totalPages, //总页数,默认为1
                                totalCount: '合计' + res.count + '条数据', // 条目总数，默认不显示
                                slideSpeed: 600, // 缓动速度。单位毫秒,默认为0无特效
                                jump: false, //是否支持跳转，默认为false
                                callback: function (page) { // 回调函数
                                    options.pageNum = page;
                                    $.comment.getCommentList(options);
                                }
                            });
                        } else {
                            $("#noComment").show();
                            $("#commentList").hide();
                            $('#pc-page').hide();
                        }
                    },
                    error: function (a, b, c) {
                        layer.alert(c, {
                            icon: 5,
                            title: "提示"
                        });
                    }
                })
            },
            getCommentList: function (options) {
                $.ajax({
                    url: ctx + "system/comment/load",
                    data: {
                        dataType: options.dataType,
                        dataId: options.dataId,
                        tenantId: options.tenantId,
                        pageNum: options.pageNum,
                        pageSize: options.pageSize
                    },
                    ansyc: false,
                    type: "get",
                    success: function (res) {
                        var tempalte = $("#commentTemplate").html();
                        $("#commentList").empty();
                        if (res.count > 0) {
                            $("#commentList").show();
                            $('#pc-page').show();
                            $("#noComment").hide();
                            $.each(res.fileList, function (index, element) {
                                var removeBtn = '';
                                if(res.currentUserId != null && element.authorIdStr != null && res.currentUserId == element.authorIdStr){
                                    removeBtn = '<span class="common-comment-item-remove" onclick="removeComment(\''+element.idStr+'\')">删除</span>'

                                }
                                var usrImg = (element.userImg == null) || (element.userImg == "") ? ctx + 'img/profile.jpg' : pathToUrl(element.userImg);
                                var one = tempalte.replace("$userName$", element.userName)
                                    .replace("$userImg$", usrImg)
                                    .replace("$commentFloor$", element.commentFloor)
                                    .replace("$content$", element.content)
                                    .replace("$publishDate$", element.publishDate)
                                    .replace("$removeComment$", removeBtn);
                                $("#commentList").append(one);
                            });
                        } else {
                            $("#noComment").show();
                            $("#commentList").hide();
                            $('#pc-page').hide();
                        }
                    },
                    error: function (a, b, c) {
                        layer.alert(c, {
                            icon: 5,
                            title: "提示"
                        });
                    }
                })
            },
            save: function (options) {
                $.ajax({
                    url: ctx + "system/comment/save",
                    ansyc: false,
                    type: "post",
                    data: {
                        content: options.content,
                        dataType: options.dataType,
                        dataId: options.dataId,
                        tenantId: options.tenantId
                    },
                    success: function (res) {
                        if (res.code == '0') { // 成功
                            // 从第一页开始重新载入评论
                            options.pageNum = 1;
                            $.comment.init(options);

                            // 评论框内容及字数清零$('#currentWordCount').html(length);
                            $('#commentTextarea').val('');
                            $('#currentWordCount').html('0');

                            layer.msg('评论成功', {time: 1000});
                        } else { // 不成功
                            layer.msg(res.msg, {time: 1000});
                        }
                    },
                    error: function (a, b, c) {
                        layer.alert(c, {
                            icon: 5,
                            title: "提示"
                        });
                    }
                })

            },
            remove: function (options) {
                layer.confirm('是否确定删除？',{
                    title: "提示",
                    skin: "skin-red",
                    btn: ['确定', '取消']//按钮
                }, function () {
                    $.ajax({
                        url: ctx + "system/comment/remove",
                        ansyc: false,
                        type: "post",
                        data: {
                            commentId: options.commentId
                        },
                        success: function (res) {
                            if (res.code == '0') { // 成功
                                // 从第一页开始重新载入评论
                                options.pageNum = 1;
                                $.comment.init(options);
                                // 评论框内容及字数清零$('#currentWordCount').html(length);
                                $('#commentTextarea').val('');
                                $('#currentWordCount').html('0');

                                layer.msg('删除成功', {time: 1000});
                            } else { // 不成功
                                layer.msg('删除失败', {time: 1000});
                            }
                        },
                        error: function (a, b, c) {
                            layer.alert(c, {
                                icon: 5,
                                title: "提示"
                            });
                        }
                    })
                });
            }
        }
    });
})(jQuery);
